﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmChangePass : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        string user, password, name,role;
        //bool b登入View;
        //bool role;
        public frmChangePass(string user,string password,string name,string role)
        {
            InitializeComponent();
            this.user = user;
            this.password = password;
            this.name = name;
            this.role = role;

        }

        private void frmChangePass_Load(object sender, EventArgs e)
        {
            //txt舊的密碼.Focus();
        }

        private void btn_changePass_Click(object sender, EventArgs e)
        {
            if((PublicClass.md5(txt舊的密碼.Text)!=password))
            {
                MessageBox.Show("舊密碼不對");
                txt舊的密碼.Focus();
                return;
            }
            if (txt新的密碼1.Text == "")
            {
                MessageBox.Show("新的密碼不可放空");
                return;
            }
            if (txt新的密碼1.Text != txt新的密碼2.Text)
            {
                MessageBox.Show("兩個新的密碼不一致");
                txt新的密碼1.Focus();
                return;
            }
            if(PublicClass.md5(txt舊的密碼.Text)==password && txt新的密碼1.Text == txt新的密碼2.Text)
            {
                SqlConnection con = new SqlConnection(connectionString);
                try
                {
                    con.Open();
                    string sp_update = "sp_使用者訊息_更新";
                    SqlCommand cmd = new SqlCommand(sp_update, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@使用者", user);
                    cmd.Parameters.AddWithValue("@密碼",PublicClass.md5(txt新的密碼1.Text));
                    cmd.Parameters.AddWithValue("@姓名", name);
                    cmd.Parameters.AddWithValue("@使用者權限代號", role);
                    cmd.Parameters.AddWithValue("@可行性", true);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("密碼已更新成功","訊息",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    con.Close();
                    this.Close();
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.Message);
                    con.Close();
                }
            }
        }
    }
}
